package net.sureon.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

@SuppressWarnings("unchecked")
public class DailyTimingDAOImpl extends SqlMapClientDaoSupport implements
		DailyTimingDAO {

	public void deleteDailyTiming(long companyID, long dailyTimingID) {
		DailyTiming dailyTiming = new DailyTiming();
		dailyTiming.setDailyTimingID(dailyTimingID);
		dailyTiming.setCompanyID(companyID);
		getSqlMapClientTemplate().delete("deleteDailyTiming", dailyTiming);
	}

	public DailyTiming getDailyTimingByID(long companyID, long dailyTimingID) {
		DailyTiming dailyTiming = new DailyTiming();
		dailyTiming.setDailyTimingID(dailyTimingID);
		dailyTiming.setCompanyID(companyID);
		return (DailyTiming) getSqlMapClientTemplate().queryForObject(
				"getDailyTimingByID", dailyTiming);
	}

	public long insertDailyTiming(DailyTiming dailyTiming) {
		Object o = getSqlMapClientTemplate().insert("insertDailyTiming",
				dailyTiming);
		if (o instanceof Long) {
			return ((Long) o);
		}
		return 0;
	}

	public int updateDailyTiming(long companyID, long dailyTimingID, java.util.Date reviewDate,
			long reviewedBy, String reviewNote, String status) {
		DailyTiming dailyTiming = new DailyTiming();
		dailyTiming.setDailyTimingID(dailyTimingID);
		dailyTiming.setReviewDate(reviewDate);
		dailyTiming.setReviewNote(reviewNote);
		dailyTiming.setStatus(status);
		dailyTiming.setReviewedBy(reviewedBy);
		dailyTiming.setCompanyID(companyID);
		return this.getSqlMapClientTemplate().update("updateDailyTiming",
				dailyTiming);
	}

	/**
	 * 用户未审核的申请(Pending)
	 */
	@SuppressWarnings("unchecked")
	public List<DailyTiming> getDailyTimingByUserNotReview(long companyID,
			long userID, int skipResults, int maxResults) {
		DailyTiming dailyTiming = new DailyTiming();
		dailyTiming.setCompanyID(companyID);
		dailyTiming.setUserID(userID);
		return this.getSqlMapClientTemplate().queryForList("getDailyTimingByUserNotReview", dailyTiming, skipResults, maxResults);
	}

	/**
	 * 用户审核过的申请（包括审核不通过和审核通过/NotApproved,Approved）
	 */
	public List<DailyTiming> getDailyTimingByUserReview(long companyID,
			long userID, int skipResults, int maxResults) {
		DailyTiming dailyTiming = new DailyTiming();
		dailyTiming.setCompanyID(companyID);
		dailyTiming.setUserID(userID);
		return this.getSqlMapClientTemplate().queryForList("getDailyTimingByUserReview", dailyTiming, skipResults, maxResults);
	}

	/**
	 * 未审核的下属申请(Pending)
	 */
	public List<DailyTiming> getDailyTimingByGroupNotReview(long companyID, List<Long> idsAsList, int skipResults, int maxResults) {
		if (idsAsList.size() > 0) {
			DailyTiming dailyTiming = new DailyTiming();
			dailyTiming.setIdsAsList(idsAsList);
			dailyTiming.setCompanyID(companyID);
			return this.getSqlMapClientTemplate().queryForList("getDailyTimingByGroupNotReview", dailyTiming, skipResults, maxResults);
		}
		return new ArrayList<DailyTiming>();
	}

	/**
	 * 审核过的下属申请（包括 NotApproved和Approved）
	 */
	public List<DailyTiming> getDailyTimingByGroupReview(long companyID, List<Long> idsAsList, int skipResults, int maxResults) {
		if (idsAsList.size() > 0) {
			DailyTiming dailyTiming = new DailyTiming();
			dailyTiming.setIdsAsList(idsAsList);
			dailyTiming.setCompanyID(companyID);
			return this.getSqlMapClientTemplate().queryForList("getDailyTimingByGroupReview", dailyTiming, skipResults, maxResults);
		}
		return new ArrayList<DailyTiming>();
	}

	public List<DailyTimingStatistic> getDailyTimingStatisticInfo(
			long companyID, long userID) {
		DailyTiming dailyTiming = new DailyTiming();
		dailyTiming.setUserID(userID);
		dailyTiming.setCompanyID(companyID);
		return this.getSqlMapClientTemplate().queryForList(
				"getDailyTimingStatisticInfo", dailyTiming);
	}

	public List<DailyTimingStatistic> getDailyTimingStatisticInfoByGroup(
			long companyID, List<Long> idsAsList) {
		DailyTiming dailyTiming = new DailyTiming();
		dailyTiming.setIdsAsList(idsAsList);
		dailyTiming.setCompanyID(companyID);
		return this.getSqlMapClientTemplate().queryForList(
				"getDailyTimingStatisticInfoByGroup", dailyTiming);
	}
//--------	
	public List<DailyTimingStatistic> getDailyTimingStatisticInfoByGroup(long companyID, List<Long> idsAsList, Date startDate, Date endDate) {
		DailyTiming dailyTiming = new DailyTiming();
		dailyTiming.setIdsAsList(idsAsList);
		dailyTiming.setCompanyID(companyID);
		dailyTiming.setStartDate(startDate);
		dailyTiming.setEndDate(endDate);
		return this.getSqlMapClientTemplate().queryForList(
				"getDailyTimingStatisticInfoByGroup", dailyTiming);
	}


	public DailyTiming getDailyTimingByWorkDate(long companyID, long userID,
			java.util.Date workDate) {
		DailyTiming dailyTiming = new DailyTiming();
		dailyTiming.setCompanyID(companyID);
		dailyTiming.setUserID(userID);
		dailyTiming.setWorkDate(workDate);
		return (DailyTiming) this.getSqlMapClientTemplate().queryForObject(
				"getDailyTimingByWorkDate", dailyTiming);

	}

	public int editDailyTiming(long companyID, long dailyTimingID, float workTime,
			float overTime, float vocationTime, float leaveTime,
			String workSummary, java.util.Date submitDate, String status) {
		DailyTiming dailyTiming = new DailyTiming();
		dailyTiming.setDailyTimingID(dailyTimingID);
		dailyTiming.setCompanyID(companyID);
		dailyTiming.setOverTime(overTime);
		dailyTiming.setWorkTime(workTime);
		dailyTiming.setVocationTime(vocationTime);
		dailyTiming.setLeaveTime(leaveTime);
		dailyTiming.setWorkSummary(workSummary);
		dailyTiming.setSubmitDate(submitDate);
		dailyTiming.setStatus(status);
		return this.getSqlMapClientTemplate().update("editDailyTiming",
				dailyTiming);
	}

}
